const query = require("../db/mysql")
const { success, error } = require("../module/module")

class Tema {
    //获取分页数据
    getList = async (ctx) => {
        let { pageIndex, pageSize } = ctx.request.body
        let sql = `select * from teamBuy where 1=1 `
        let countSql = 'select count(id) as total from teamBuy '
        let total = await query(countSql)
        if (pageIndex && pageSize) {
            sql += `limit ${(pageIndex * 1 - 1) * pageSize * 1},${pageSize * 1} `
        }
        let data = await query(sql)
        ctx.body = {
            total: total[0].total,
            data,
            code: 1
        }
    }
    //搜索团购
    searchTeam = async (ctx) => {
        let { keyword } = ctx.request.body
        let sql = `select * from teamBuy where 1=1 `
        if (keyword) {
            sql += `and	buildname like "%${keyword}%" `
        }
        let data = await query(sql)
        ctx.body = success(data, "查找成功")
    }

    //删除
    deleteId = async (ctx) => {
        let { id } = ctx.request.body
        let sql = `delete  from teamBuy where 1=1 and id=${id} `
        let data = await query(sql)

        if (data.affectedRows === 1) {
            ctx.body = success(data, '删除成功')
        }

    }
    //状态筛选
    selectState = async (ctx) => {
        let { state } = ctx.request.body
        let sql = `select * from teamBuy where 1=1 `

        if (state) {
            sql += `and state like "%${state}%" `
        }
        let data = await query(sql)
        ctx.body = success(data, '筛选成功')
    }

}

module.exports = new Tema()